package com.tencent.hummingbird.portal.controller;


import com.tencent.hummingbird.portal.mapper.UserMapper;
import com.tencent.hummingbird.portal.pojo.User;
import com.tencent.hummingbird.portal.r.R;
import com.tencent.hummingbird.portal.service.IUserService;
import com.tencent.hummingbird.portal.vo.ReSetVO;
import com.tencent.hummingbird.portal.vo.UserInfoVO;
import com.tencent.hummingbird.portal.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxb
 * @since 2024-01-10
 */
@Slf4j
@RestController
@RequestMapping("/portal/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IUserService userService;
    @GetMapping("/get")
    @PreAuthorize("hasAuthority('/abc')")
    public User get(){
        return (userMapper.selectById(4));
    }
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/portal/user/list')")
    public List<User> list(){
        return (userMapper.selectList(null));
    }
    @GetMapping("/teachers")
    public R<List<User>> Teachers(){
         List<User> teachers = userService.getTeachers();
         return R.ok(teachers);
    }
    @GetMapping("/me")
    public R <UserVO> myinfo(@AuthenticationPrincipal UserDetails userDetails){
       UserVO userVO =  userService.getCurrentUserVo(userDetails.getUsername());
       return R.ok(userVO);
    }
    @PostMapping("/reset")
    public  R resetPassword(@AuthenticationPrincipal UserDetails userDetails, @RequestBody @Validated ReSetVO reSetVO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return R.unproecsableEntity(bindingResult.getFieldError().getDefaultMessage());
        }
        if(!reSetVO.getNewPass().equals(reSetVO.getConfirmPasswd())){
            return R.unproecsableEntity("两次密码不一致");
        }
        userService.resetPassword(userDetails.getUsername(),reSetVO);
        return R.created("修改成功");
    }

    @GetMapping("/myInfo")
    public R<UserInfoVO> myInfo(@AuthenticationPrincipal UserDetails userDetails){
        UserInfoVO userInfoVO = userService.getUserInfoVoByUserName(userDetails.getUsername());
        return R.ok(userInfoVO);
    }

    @PostMapping("/updateInfo")
    public R updateInfo(@AuthenticationPrincipal UserDetails userDetails,@RequestBody @Validated UserInfoVO userInfoVO,BindingResult result){
        if(result.hasErrors()){
            return R.unproecsableEntity(result.getFieldError().getDefaultMessage());
        }
        log.info("userInfoVO:{}",userInfoVO);
        userService.UpdateUserInfo(userDetails.getUsername(),userInfoVO);
        return R.created("修改成功");
    }
}
